package java_thinking.unit_15._15_4;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description :   Set例子
 * @author: cww
 * @DateTime: 2019-04-02 22:49
 */

public class Sets {
    public static <T> Set<T> union(Set<T> a, Set<T> b) {
        Set<T> result = new HashSet<>(a);
        //合并参数
        result.addAll(b);
        return result;
    }

    public static <T> Set<T> intersection(Set<T> a, Set<T> b) {
        Set<T> result = new HashSet<>(a);
        //返回共有的部分
        result.retainAll(b);
        return result;
    }
    public static <T> Set<T> difference(Set<T> a, Set<T> b) {
        Set<T> result = new HashSet<>(a);
        //移除a中b的元素
        result.removeAll(b);
        return result;
    }

    /**
     * 返回Set除了交集之外的所有元素
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T> Set<T> complement(Set<T> a, Set<T> b) {
        return difference(union(a,b),intersection(a,b));
    }
}
